package com.itheima.web;

import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.constant.MessageConstant;
import com.itheima.entity.Result;
import com.itheima.pojo.Menu;
import com.itheima.service.MenuService;
import com.itheima.service.RoleService;
import com.itheima.service.UserService;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.userdetails.User;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.security.Security;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 菜单 Controller
 * @author HY
 * @version 2/11/2020
 */
@RestController
@RequestMapping("menu")
public class MenuController {
    @Reference
    MenuService menuService;
    @Reference
    RoleService roleService;
    @Reference
    UserService userService;

    /**
     * 获取菜单
     * @param id     用户id
     * @param roleId 角色id
     * @return Result 对象
     */
    @RequestMapping("getMenus")
    public Result getMenus(@RequestParam(name = "id", required = false, defaultValue = "0") Integer id, Integer roleId) {
        // 查询全部菜单
        List<Menu> menus = menuService.getMenus();
        // 角色分配的菜单id
        List<Integer> menuIds = roleService.getMenus(roleId);
        Map<String, List<?>> map = new HashMap<>();
        map.put("menus", menus);
        map.put("menuIds", menuIds);
        return new Result(true, MessageConstant.GET_MENU_SUCCESS, map);
    }

    /**
     * 添加菜单
     * @param menu 菜单对象
     * @return Result 对象
     */
    @RequestMapping("addMenu")
    public Result addMenu(@RequestBody Menu menu) {
        try {
            menuService.addMenu(menu);
            return new Result(true, MessageConstant.ADD_MENU_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.ADD_MENU_FAIL);
        }
    }

    /**
     * 编辑菜单
     * @param menu 菜单对象
     * @return Result 对象
     */
    @RequestMapping("editMenu")
    public Result editMenu(@RequestBody Menu menu) {
        try {
            menuService.editMenu(menu);
            return new Result(true, MessageConstant.EDIT_MENU_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.EDIT_MENU_FAIL);
        }
    }

    /**
     * 删除菜单
     * @param id 菜单id
     * @return Result 对象
     */
    @RequestMapping("deleteMenu")
    public Result deleteMenu(Integer id) {
        try {
            menuService.deleteMenu(id);
            return new Result(true, MessageConstant.DELETE_MENU_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.DELETE_MENU_FAIL);
        }
    }

    @RequestMapping("getUserMenu")
    public Result getUserMenu(){
        try {
            User user = (User) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
            String username = user.getUsername();
            Integer id = userService.findIdByUsername(username);
            List<Menu> menuList = menuService.getUserMenu(id);
            return new Result(true,MessageConstant.GET_MENU_SUCCESS,menuList);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.GET_MENU_FAIL);
        }
    }
}
